<?php
/**
 * Created by PHPStorm
 * User: xiest
 * Date: 2021/10/8
 * Email: <939369942@qq.com>
 */
namespace app\controller\admin\info;

use app\BaseController;
use app\common\repositories\user\UserBalanceRepository;
use app\common\repositories\user\UserExtractRepository as repository;
use app\common\repositories\user\UserRepository;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;

class Extract extends BaseController
{
    protected repository $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    public function lst()
    {
        [$page, $limit] = $this->page();
        $where = $this->request->params(['status']);
        return app('json')->success($this->repository->getAdminLst($where,$page,$limit));
    }


    public function audit($id)
    {
        $data = $this->repository->get($id);

        if(!$data){
            throw new ValidateException('数据不存在');
        }

        if($data->status != 0){
            throw new ValidateException('当前提现请求已处理');
        }


        $status = input('status',1);


        Db::transaction(function ()use($data,$status){

            $data->save(['status'=>$status]);

            if($status == -1){
                /**@var $balanceMake UserBalanceRepository**/
                $balanceMake = app()->make(UserBalanceRepository::class);
                $balanceMake->recordBalance($data->uid,1,'extract_reject','提现失败退回余额',$data->price);
            }
        });

        return success('审核成功');
    }


}